#include"BitMap.h"
#include<iostream>
#include<bitset>
#include<math.h>
BitMap::BitMap(int max)
{
	m_nSize = floor(max / 8);
	m_nMaxNum = max;
	m_pData = new char[m_nSize];
	memset(m_pData, 0, m_nSize);
}

BitMap::~BitMap()
{
	if (m_pData != nullptr) {
		delete[] m_pData;
		m_pData = nullptr;
	}
}

bool BitMap::insertNum(int num)
{
	if (num > m_nMaxNum)
		return false;
	if (findNum(num))
		return false;
	int pos = num >> 3;
	int move = num % 8;
	char mask = 1 << move;
	m_pData[pos] |= mask;
}

bool BitMap::delNum(int num)
{
	if (num > m_nMaxNum)
		return false;
	if (!findNum(num))
		return false;
	int pos = num >> 3;
	int move = num % 8;
	char mask = 1 << move;
	m_pData[pos] ^= mask;
}

bool BitMap::findNum(int num)
{
	if (num > m_nMaxNum)
		return false;
	int pos = num >> 3;
	int move = num % 8;
	char mask = 1 << move;
	return (m_pData[pos] & mask) >> move & 1 ? true : false;
}

void BitMap::PrintBit()
{
#ifdef  DEBUG
	for (int i = 0; i < m_nSize; i++) {
		std::cout << i << ": " << std::bitset<8>(m_pData[i]) << std::endl;
}
#endif //  DEBUG
}